package com.gjm.demo.CalcController;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/calc")
@RestController
public class CalcController {

    @RequestMapping("/sum")
    public String add(Integer num1, Integer num2) {
        if (num1 == null || num2 == null) {
            return "请输入完整数据";
        }

        Integer sum = num1 + num2;
        return "<h1>计算结果：" + sum + "</h1>";
    }

    @RequestMapping("/m1")
    public String calc(Integer num1, Integer num2, String method) {
        if (num1 == null || num2 == null) {
            return "请输入完整数据";
        }

        if (method == null) {
            return "选择计算方式";
        }

        Integer ret = null;
        switch (method) {
            case "+":
                ret = num1 + num2;
                break;
            case "-":
                ret = num1 - num2;
                break;
            case "*":
                ret = num1 * num2;
                break;
            case "/":
                if (num2 == 0) {
                    return "<h1>除数不能为0</h1>";
                }
                ret = num1 / num2;
                break;
        }

        return "<h1>计算结果：" + ret + "</h1>";
    }
}